package br.com.async.controller.room;

import br.com.async.application.CategoryApplication;
import br.com.async.application.PersonApplication;
import br.com.async.application.RoomApplication;
import br.com.async.domain.room.Room;
import br.com.async.domain.room.RoomStatus;
import br.com.async.infrastructure.util.ApplicationContextRegisterCore;
import br.com.async.view.util.AsyncMessage;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;

@Resource
public class RoomController {

	private final Result result;
	private RoomApplication roomApp = ApplicationContextRegisterCore.getBean("roomApplication", RoomApplication.class);
	private CategoryApplication catApp = ApplicationContextRegisterCore.getBean("categoryApplication", CategoryApplication.class);
	private PersonApplication respApp = ApplicationContextRegisterCore.getBean("personApplication", PersonApplication.class);

	public RoomController(Result result) {
		this.result = result;
	}
	
	@Path("/room")
	public void roomPanel() {
		
	}
	
	@Path("/salas")
	public void roomList(){
		result.include("pageRoom","ui-btn-active");
		result.include("list",roomApp.list());
		
		result.forwardTo(this).roomPanel();
	}
	
	@Path("/salas/busca")
	public void roomSearch(String roomName, String offset){
		result.include("pageRoom","ui-btn-active");
		result.include("list",roomApp.listRoomByCriterions(roomName, null, null, Integer.parseInt(offset), 30).getItems());
		result.include("roomName",roomName);
		result.forwardTo(this).roomPanel();
	}
	
	@Path("/nova-sala")
	public void roomWindow(){
		result.include("catList",catApp.list());
	}

	
	@Post("/room/persist")
	public void persist(Room room, String categoryCode){
		
	
		try{
			room.setCategory(catApp.findByCode(Integer.parseInt(categoryCode)));
			room.setRoomStatus(RoomStatus.FREE);
			roomApp.persist(room);	
			result.use(Results.json()).from(AsyncMessage.ROOM_PERSIST.toString(), "resultMessage").serialize();
		}catch(Exception e){
			result.use(Results.json()).from(AsyncMessage.ROOM_PERSIST_ERROR.toString(), "resultMessage").serialize();
		}
		result.use(Results.nothing());
	}
	
	private void persist(){
		
	}
}
